package com.right.health.app.utils;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.Locale;

/*
 * Created by hopeliao on 2019-12-30
 *
 */public class TimeUtils {
    public static final String DEFAULT_FORMAT = "yyyy-MM-dd HH:mm:ss";

    public static int getHour(String time) {
        return getHour(DEFAULT_FORMAT, time);
    }

    public static int getHour(String format, String time) {
        SimpleDateFormat sdf = new SimpleDateFormat(format, Locale.getDefault());
        try {
            Date date = sdf.parse(time);
            Calendar calendar = Calendar.getInstance();
            calendar.setTime(date);

            return calendar.get(Calendar.HOUR_OF_DAY);
        } catch (ParseException e) {
            e.printStackTrace();
        }
        return 0;
    }


    public static int getWeek(String time) {
        try {
            Calendar calendar = Calendar.getInstance();
            calendar.setTimeInMillis(Long.parseLong(time));
            int week = calendar.get(Calendar.DAY_OF_WEEK) - 1;
            return week == 0 ? 7 : week;
        } catch (Exception e) {
            e.printStackTrace();
        }
        return 0;
    }

    public static String[] getWeekDate() {
        SimpleDateFormat sdf = new SimpleDateFormat("MM.dd", Locale.getDefault());
        Calendar calendar = Calendar.getInstance();
        // 获取本周的第一天
        int firstDayOfWeek = calendar.getFirstDayOfWeek();
        String[] weekArray = new String[7];
        if (firstDayOfWeek==1){
            for (int i = 6; i >=0; i--) {
                weekArray[i] = sdf.format(calendar.getTime());
                calendar.add(Calendar.DAY_OF_MONTH, -1);
            }
        }else {
            calendar.set(Calendar.DAY_OF_WEEK, firstDayOfWeek);
            for (int i = 0; i < 7; i++) {
                calendar.add(Calendar.DAY_OF_MONTH, 1);
                weekArray[i] = sdf.format(calendar.getTime());

            }
        }
        return weekArray;
    }

    public static String[] getWeekDateWithBlank() {
        String[]array=new String[8];
        array[0] = "";
        System.arraycopy(getWeekDate(),0,array,1,7);
        return array;
    }

    public static String[] getMonthDate() {
        String[] array = new String[2];
        Calendar calendar = Calendar.getInstance();
        SimpleDateFormat sdf = new SimpleDateFormat("MM.dd", Locale.getDefault());
        calendar.set(Calendar.DATE, 1);
        array[0] = sdf.format(calendar.getTime());
        calendar.add(Calendar.MONTH, 1);
        calendar.add(Calendar.DATE, -1);
        array[1] = sdf.format(calendar.getTime());
        return array;
    }

    public static int getDay(String time) {
        try {
            Calendar calendar = Calendar.getInstance();
            calendar.setTimeInMillis(Long.parseLong(time));
            return calendar.get(Calendar.DAY_OF_MONTH);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return 0;
    }

    public static int getDayNumInMonth() {
        Calendar calendar = Calendar.getInstance();
        SimpleDateFormat sdf = new SimpleDateFormat("MM.dd", Locale.getDefault());
        calendar.set(Calendar.DATE, 1);
        calendar.add(Calendar.MONTH, 1);
        calendar.add(Calendar.DATE, -1);
        return calendar.get(Calendar.DAY_OF_MONTH);
    }

    public static String[] getPastWeek(){
        Calendar calendar = Calendar.getInstance();
        String[] array=new String [7];
        SimpleDateFormat sdf = new SimpleDateFormat("MM.dd", Locale.getDefault());
        for (int i = 6; i >=0 ; i--) {
            array[i] = sdf.format(calendar.getTime());
            calendar.add(Calendar.DAY_OF_MONTH, -1);

        }
        return array;
    }

    public static String[] getPastMonth(){
        Calendar calendar = Calendar.getInstance();
        String[] array=new String [30];
        SimpleDateFormat sdf = new SimpleDateFormat("MM.dd", Locale.getDefault());
        for (int i = 29; i >=0 ; i--) {
                array[i] = sdf.format(calendar.getTime());
            calendar.add(Calendar.DAY_OF_MONTH, -1);

        }
        return array;
    }

    public static String getMonthAndDay(long time){
        Calendar calendar = Calendar.getInstance();
        calendar.setTimeInMillis(time);
        SimpleDateFormat sdf = new SimpleDateFormat("MM.dd", Locale.getDefault());
        return sdf.format(calendar.getTime());
    }

}
